package org.streamerstudio.ui.properties;

import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.UpdateValueStrategy;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.emf.databinding.EMFObservables;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.TableWrapData;
import org.eclipse.ui.forms.widgets.TableWrapLayout;
import org.streamerstudio.model.gstreamer.registry.GstElement;
import org.streamerstudio.model.gstreamer.registry.RegistryPackage.Literals;

public class ElementPropertyComposite extends Composite {
	private DataBindingContext m_bindingContext;

	private final FormToolkit toolkit = new FormToolkit(Display.getCurrent());
	private Text textAuthor;
	private Text textGlibClass;
	private Text textLongName;
	private GstElement model;

	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public ElementPropertyComposite(Composite parent, int style) {
		super(parent, style);

		createContents();
	}
	
	private void createContents() {
		toolkit.adapt(this);
		toolkit.paintBordersFor(this);
		{
			TableWrapLayout tableWrapLayout = new TableWrapLayout();
			tableWrapLayout.numColumns = 2;
			setLayout(tableWrapLayout);
		}
		// -- component creation
		toolkit.createLabel(this, "Author", SWT.NONE);
		// -- component creation
		textAuthor = toolkit.createText(this, "New Text", SWT.NONE);
		textAuthor.setText("");
		textAuthor.setLayoutData(new TableWrapData(TableWrapData.FILL_GRAB, TableWrapData.TOP, 1, 1));
		// -- component creation
		toolkit.createLabel(this, "Glib Class", SWT.NONE);
		// -- component creation
		textGlibClass = toolkit.createText(this, "New Text", SWT.NONE);
		textGlibClass.setText("");
		textGlibClass.setLayoutData(new TableWrapData(TableWrapData.FILL_GRAB, TableWrapData.TOP, 1, 1));
		// -- component creation
		toolkit.createLabel(this, "Long Name", SWT.NONE);
		// -- component creation
		textLongName = toolkit.createText(this, "New Text", SWT.NONE);
		textLongName.setText("");
		textLongName.setLayoutData(new TableWrapData(TableWrapData.FILL_GRAB, TableWrapData.TOP, 1, 1));
		
		m_bindingContext = initDataBindings();
	}
	
	
	protected DataBindingContext initDataBindings() {
		DataBindingContext bindingContext = new DataBindingContext();
		//
		IObservableValue textAuthorObserveTextObserveWidget = SWTObservables.observeText(textAuthor, SWT.NONE);
		IObservableValue modelAuthorObserveValue = EMFObservables.observeValue(model, Literals.GST_ELEMENT__AUTHOR);
		bindingContext.bindValue(textAuthorObserveTextObserveWidget, modelAuthorObserveValue, new UpdateValueStrategy(UpdateValueStrategy.POLICY_ON_REQUEST), new UpdateValueStrategy(UpdateValueStrategy.POLICY_ON_REQUEST));
		//
		IObservableValue textGlibClassObserveTextObserveWidget = SWTObservables.observeText(textGlibClass, SWT.NONE);
		IObservableValue modelGlibClassObserveValue = EMFObservables.observeValue(model, Literals.GST_ELEMENT__GLIB_CLASS);
		bindingContext.bindValue(textGlibClassObserveTextObserveWidget, modelGlibClassObserveValue, new UpdateValueStrategy(UpdateValueStrategy.POLICY_ON_REQUEST), new UpdateValueStrategy(UpdateValueStrategy.POLICY_ON_REQUEST));
		//
		IObservableValue textLongNameObserveTextObserveWidget = SWTObservables.observeText(textLongName, SWT.NONE);
		IObservableValue modelLongNameObserveValue = EMFObservables.observeValue(model, Literals.GST_ELEMENT__LONG_NAME);
		bindingContext.bindValue(textLongNameObserveTextObserveWidget, modelLongNameObserveValue, new UpdateValueStrategy(UpdateValueStrategy.POLICY_ON_REQUEST), new UpdateValueStrategy(UpdateValueStrategy.POLICY_ON_REQUEST));
		//
		return bindingContext;
	}
	
	public void setInput(GstElement element){
		this.model = element;
		m_bindingContext = initDataBindings();
	}
	
	public void update(){
		//m_bindingContext.updateModels();
		m_bindingContext.updateTargets();
	}
}
